<template>
	<view class="upload_info">
		<u-upload
			:width="130"
			:height="130"
			:show-progress="false"
			:name="'image'"
			@on-uploaded="uploaded"
			:header="header"
			:max-count="maxnum"
			:action="action"
			:file-list="imglist"
		></u-upload>
	</view>
</template>

<script>
import base from '@/common/baseUrl';
import {mapState, mapMutations} from 'vuex';
import util from '@/common/util.js';
export default {
	data() {
		return {
			action: base.baseUrl + '/index/order/imgUpdate',
			header: {},
			imglist: []
		};
	},
	props: {
		// imglist: {
		// 	type: Array,
		// 	default:[]
		// },
		maxnum: {
			type: Number,
			default: 5
		}
	},
	computed: {
		...mapState({
			token: state => state.token
		})
	},
	created() {
		console.log(111)
		
		let mid = uni.getStorageSync('mall_id');
		let is_wx = uni.getStorageSync('is_wx');
		let UserType = 3;
		// #ifdef MP-WEIXIN
		UserType = 1;
		// #endif
		// #ifndef MP-WEIXIN
		let Browser = util.getBrowser();
		if (Browser == '微信' && is_wx) {
			UserType = 2;
		}
		// #endif
		this.header = {
			Authorization: this.token,
			mid: mid,
			UserType: UserType
		};
	},
	methods: {
		onDeleteImg(index) {
			this.imglist.splice(index, 1);
		},
		uploaded(lists, name) {
			let list = [];
			lists.forEach(a => {
				if (a.response && a.response.msg) list.push(a.response.msg[0]);
			});
			this.$emit('getimg', list);
		},
		uploadimg(data) {
			wx.showLoading({
				title: ''
			});
			var token = uni.getStorageSync('token') || null;

			var that = this,
				i = data.i ? data.i : 0, //当前上传的哪张图片
				success = data.success ? data.success : 0, //上传成功的个数
				fail = data.fail ? data.fail : 0; //上传失败的个数

			wx.uploadFile({
				url: data.url,
				filePath: data.path[i],
				name: 'image', //这里根据自己的实际情况改
				header: {
					Authorization: token
				},
				formData: {
					// 'type': 'evaluate',
					// 'imagesize': ''
				}, //这里是上传图片时一起上传的数据
				success: resp => {
					if (resp.statusCode == 200) {
						var json = JSON.parse(resp.data);
						console.log(json);
						var url = base.baseUrl + '/' + json.msg.replace('\\', '/');
						success++; //图片上传成功，图片上传成功的变量+1
						console.log(this.imglist);
						that.imglist.push(url);
						console.log(this.imglist);
					}

					// console.log(resp)
					// console.log(i);
					//这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
				},
				fail: res => {
					fail++; //图片上传失败，图片上传失败的变量+1
					console.log('fail:' + i + 'fail:' + fail);
				},
				complete: () => {
					console.log(i);
					i++; //这个图片执行完上传后，开始上传下一张
					if (i == data.path.length) {
						//当图片传完时，停止调用
						wx.hideLoading();
						//         that.setData({
						//           imglist: that.imglist

						//         })
						that.$emit('getimg', this.imglist);
						console.log('执行完毕');
						console.log('成功：' + success + ' 失败：' + fail);
					} else {
						//若图片还没有传完，则继续调用函数
						console.log(i);
						data.i = i;
						data.success = success;
						data.fail = fail;
						that.uploadimg(data);
					}
				}
			});
		},
		bindChooiceProduct() {
			var t = this;
			let count = this.maxnum - t.imglist.length;
			console.log(count);
			uni.chooseImage({
				count: count,
				sizeType: ['compressed'],
				sourceType: ['album', 'camera'],
				success(i) {
					t.uploadimg({
						url: base.baseUrl + '/index/order/imgUpdate',
						path: i.tempFilePaths
					});
					// for (var c = 0; c < i.tempFilePaths.length; c++) {

					// }
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
